package com.zc.emos.wx.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author 曾川
 * @version 1.0.0
 * @ClassName ThreadPoolConfig.java
 * @Description TODO
 * @createTime 2022年01月10日 17:41:00
 */
@Configuration
public class ThreadPoolConfig {

    @Bean("AsyncTaskExecutor")
    public AsyncTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor excutor = new ThreadPoolTaskExecutor();
        excutor.setCorePoolSize(8);// 设置核心线程数  cpu 内核的2倍
        excutor.setMaxPoolSize(16);// 设置最大线程数
        excutor.setQueueCapacity(32);// 设置队列容量
        excutor.setKeepAliveSeconds(60);// 设置线程活跃时间（秒）
        excutor.setThreadNamePrefix("task-");// 设置默认线程名称前缀
        // 设置拒绝策略
        excutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        excutor.initialize();
        return excutor;
    }
}
